#pragma once
#include <iostream>
#include <string>
#include "jsoncpp/json/json.h"

const std::string blank_space_sep = " ";
const std::string protocol_sep = "\n";
std::string Enable(std::string &in)
{
    std::string s = std::to_string(in.size());
    s += protocol_sep;
    s += in;
    s += protocol_sep;
    return s;
}
bool Deable(std::string &package, std::string *out)
{
    size_t pos = package.find(protocol_sep);
    if (pos == std::string::npos)
        return false;
    size_t len = std::stoi(package.substr(0, pos));
    int total = len + pos + 2;
    if (total > package.size())
        return false;
    *out = package.substr(pos + 1, len);
    // std::cout << "-----------------sss----------------" << std::endl;
    package.erase(0, total);
    return true;
}
class Request
{
public:
    Request(int x, int y, int op) : x_(x), y_(y), op_(op)
    {
    }
    Request() {}
    void DebugPrint()
    {
        std::cout << "新请求生成： " << x_ << op_ << y_ << "=?" << std::endl;
    }
    bool Serialize(std::string *out) //
    {
#ifdef Myself
        *out = std::to_string(x_);
        *out += blank_space_sep;
        *out += op_;
        *out += blank_space_sep;

        *out += std::to_string(y_);
        return true;
#else
        Json::Value v;
        v["x"] = x_;
        v["y"] = y_;
        v["op"] = op_;
        Json::FastWriter w;
        *out = w.write(v);
        // std::cout << *out << std::endl;
        return true;
#endif
    }

    bool Deserialize(std::string &in) // x op y
    {
#ifdef Myself

        size_t left = in.find(blank_space_sep);
        if (left == std::string::npos)
            return false;
        std::string part_left = in.substr(0, left);
        size_t right = in.rfind(blank_space_sep);
        if (right == std::string::npos)
            return false;
        std::string part_right = in.substr(right + 1);
        op_ = in[left + 1];
        x_ = std::stoi(part_left);
        y_ = std::stoi(part_right);
        return true;
#else
        Json::Value v;
        Json::Reader r;
        r.parse(in, v);
        x_ = v["x"].asInt();
        y_ = v["y"].asInt();
        op_ = v["op"].asInt();
        return true;
#endif
    }

public:
    int x_;
    int y_;
    char op_;
};

class Response
{
public:
    Response(int result, int code) : result_(result), code_(code)
    {
    }
    Response() {}
    void DebugPrint()
    {
        std::cout << "响应： result:" << result_ << "code:" << code_ << std::endl;
    }
    bool Serialize(std::string *out) //
    {
#ifdef Myself

        *out = std::to_string(result_);
        *out += blank_space_sep;
        *out += std::to_string(code_);
        return true;
#else

        Json::Value v;
        v["result"] = result_;
        v["code"] = code_;
        Json::FastWriter w;
        *out = w.write(v);
        return true;
#endif
    }

    bool Deserialize(std::string &in) // result_ code_
    {
#ifdef Myself

        size_t pos = in.find(blank_space_sep);
        if (pos == std::string::npos)
            return false;
        std::string part_left = in.substr(0, pos);
        std::string part_right = in.substr(pos + 1);
        result_ = std::stoi(part_left);
        code_ = std::stoi(part_right);
        return true;
#else
        std::cout << in << std::endl;
        Json::Value v;
        Json::Reader r;
        r.parse(in, v);
        result_ = v["result"].asInt();
        code_ = v["code"].asInt();
        return true;

#endif
    }

public:
    int result_;
    int code_;
};